package com.night.icm.controller.business;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.TermType;
import com.night.icm.model.business.CourseTeacherDTO;
import com.night.icm.service.business.service.CourseTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * CourseTeacherController
 * 教务员权限
 *
 * @author Liao ZhiYong
 * @date 2020/2/14
 */
@Api(tags = "开课管理")
@RestController
@RequestMapping("/api/icm-course-teacher")
public class CourseTeacherController {

    private final CourseTeacherService courseTeacherService;

    @Autowired
    public CourseTeacherController(CourseTeacherService courseTeacherService) {
        this.courseTeacherService = courseTeacherService;
    }

    @ApiOperation(value = "分页查找开课信息")
    @GetMapping(value = "")
    public PageDataDTO<CourseTeacherDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "所属年度") @RequestParam(value = "year", required = false) String year,
            @ApiParam(value = "所属学期") @RequestParam(value = "termType", required = false) TermType termType,
            @ApiParam(value = "课程名称") @RequestParam(value = "courseName", required = false) String courseName,
            @ApiParam(value = "授课教师") @RequestParam(value = "teacherName", required = false) String teacherName,
            @ApiParam(value = "是否开课") @RequestParam(value = "isOpen", required = false) Boolean isOpen
    ) {
        return this.courseTeacherService.findOnePage(page, size, sort, year, termType, courseName, teacherName, isOpen);
    }

    @ApiOperation(value = "添加开课信息")
    @PostMapping(value = "")
    public CourseTeacherDTO create(
            @ApiParam(value = "开课信息", required = true) @RequestBody @Validated CourseTeacherDTO courseTeacherDTO
    ) {
        return this.courseTeacherService.create(courseTeacherDTO);
    }

    @ApiOperation(value = "根据id删除开课信息")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "开课id", required = true) @PathVariable(name = "id") String id) {
        this.courseTeacherService.delete(id);
    }

    @ApiOperation(value = "更新开课信息")
    @PutMapping(value = "/{id}")
    public CourseTeacherDTO update(
            @ApiParam(value = "开课id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "开课信息", required = true) @RequestBody @Validated CourseTeacherDTO courseTeacherDTO
    ) {
        return this.courseTeacherService.update(id, courseTeacherDTO);
    }

    @ApiOperation(value = "根据id查找开课信息")
    @GetMapping(value = "/{id}")
    public CourseTeacherDTO findById(@ApiParam(value = "开课信息id", required = true) @PathVariable(name = "id") String id) {
        return this.courseTeacherService.findById(id);
    }

    @ApiOperation(value = "查找所有开课课程")
    @GetMapping(value = "/all")
    public List<CourseTeacherDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.courseTeacherService.findAll(param, pattern);
    }
}